//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// ChartBar3DView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "ChartBar3DView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChartBar3DView

IMPLEMENT_DYNCREATE(CChartBar3DView, CBCGPChartExampleView)

CChartBar3DView::CChartBar3DView()
	: CBCGPChartExampleView(CChartBar3DView::IDD)
{
	//{{AFX_DATA_INIT(CChartBar3DView)
	m_nFillGradientType = 4;
	m_bShowDataLabels = FALSE;
	m_nDataLabelPosition = 0;
	m_nTransparency = 10;
	m_bShowWalls = TRUE;
	m_bThickWalls = TRUE;
	//}}AFX_DATA_INIT
}

CChartBar3DView::~CChartBar3DView()
{
}

void CChartBar3DView::DoDataExchange(CDataExchange* pDX)
{
	CBCGPChartExampleView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChartBar3DView)
	DDX_Control(pDX, IDC_ROTATE, m_wndRotate);
	DDX_Control(pDX, IDC_DATA_LABEL_POSITION, m_wndDataLabelPosition);
	DDX_Control(pDX, IDC_CHART, m_wndChart);
	DDX_CBIndex(pDX, IDC_FILL_GRADIENT_TYPE, m_nFillGradientType);
	DDX_Check(pDX, IDC_DATA_LABELS, m_bShowDataLabels);
	DDX_CBIndex(pDX, IDC_DATA_LABEL_POSITION, m_nDataLabelPosition);
	DDX_Control(pDX, IDC_TRANSPARENCY, m_wndTransparency);
	DDX_Slider(pDX, IDC_TRANSPARENCY, m_nTransparency);
	DDX_Check(pDX, IDC_SHOW_WALLS, m_bShowWalls);
	DDX_Control(pDX, IDC_THICK_WALLS, m_wndThickWalls);
	DDX_Check(pDX, IDC_THICK_WALLS, m_bThickWalls);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChartBar3DView, CBCGPChartExampleView)
	//{{AFX_MSG_MAP(CChartBar3DView)
	ON_BN_CLICKED(IDC_DATA_LABELS, OnUpdateChart)
	ON_BN_CLICKED(IDC_ROTATE, OnRotate)
	ON_WM_HSCROLL()
	ON_CBN_SELENDOK(IDC_DATA_LABEL_POSITION, OnUpdateChart)
	ON_CBN_SELENDOK(IDC_FILL_GRADIENT_TYPE, OnUpdateChart)
	ON_BN_CLICKED(IDC_SHOW_WALLS, OnUpdateChart)
	ON_BN_CLICKED(IDC_THICK_WALLS, OnUpdateChart)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartBar3DView diagnostics

#ifdef _DEBUG
void CChartBar3DView::AssertValid() const
{
	CBCGPChartExampleView::AssertValid();
}

void CChartBar3DView::Dump(CDumpContext& dc) const
{
	CBCGPChartExampleView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChartBar3DView message handlers

void CChartBar3DView::OnInitialUpdate() 
{
	CBCGPChartExampleView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}

	m_bIsReady = TRUE;

	m_wndTransparency.SetRange(0, 100, TRUE);

	m_wndRotate.GetRotationObject()->SetAutorepeatMode(100);
	m_wndRotate.GetRotationObject()->SetColorTheme(CBCGPRotationObject::BCGP_COLOR_THEME_VISUAL_MANAGER);

	m_wndRotate.GetRotationObject()->EnablePart(CBCGPRotationObject::BCGP_ROTATION_CLOCKWISE, FALSE);
	m_wndRotate.GetRotationObject()->EnablePart(CBCGPRotationObject::BCGP_ROTATION_COUNTER_CLOCKWISE, FALSE);

	m_wndRotate.GetRotationObject()->EnableFlatIcons();

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	BCGPChartCategory category = 
		(m_Feature == CDemoFeature::BCGP_StackedBar3D || m_Feature == CDemoFeature::BCGP_StackedBar1003D || 
		m_Feature == CDemoFeature::BCGP_Bar3D || m_Feature == CDemoFeature::BCGP_ClusteredBar3D) ? 
			BCGPChartBar3D : 
			BCGPChartColumn3D;

	BCGPChartType type = BCGP_CT_SIMPLE;

	switch (m_Feature)
	{
	case CDemoFeature::BCGP_StackedColumn3D:
	case CDemoFeature::BCGP_StackedBar3D:
	case CDemoFeature::BCGP_StackedColumnSideBySide3D:
	case CDemoFeature::BCGP_StackedColumnManhattan3D:
		type = BCGP_CT_STACKED;
		break;

	case CDemoFeature::BCGP_StackedColumn1003D:
	case CDemoFeature::BCGP_StackedBar1003D:
		type = BCGP_CT_100STACKED;
		break;
	}

	pChart->SetChartType(category, type);

	if (m_Feature == CDemoFeature::BCGP_ClusteredBar3D || m_Feature == CDemoFeature::BCGP_ClusteredColumn3D)
	{
		pChart->GetDiagram3D()->SetGrouped(TRUE, FALSE);
	}

	if (m_Feature == CDemoFeature::BCGP_StackedColumnManhattan3D)
	{
		pChart->SetChartTitle(_T("Manhattan Stacked Chart in 3D"));
		pChart->SetLegendPosition(BCGPChartLayout::LP_NONE);

		pChart->GetDiagram3D()->SetExplicitGrouping(CBCGPChartDiagram3D::EG_NOT_GROUPED);

		for (int i = 0; i < 9; i++)
		{
			CBCGPChartSeries* pBarSeries = pChart->CreateSeries(_T(""));
			pBarSeries->m_bIncludeSeriesToLegend = FALSE;
			for (int j = 0; j < 3; j++)
			{
				pBarSeries->AddDataPoint(ceil(Rand(2.0, 15.0)));
				pBarSeries->SetGroupID(i / 3);
			}
		}
	}
	else
	{
		pChart->SetChartTitle(_T("Vehicle Expenses"));

		CBCGPChartBarSeries* pBarSeries1 = DYNAMIC_DOWNCAST(CBCGPChartBarSeries, pChart->CreateSeries(_T("Other")));
		CBCGPChartBarSeries* pBarSeries2 = DYNAMIC_DOWNCAST(CBCGPChartBarSeries, pChart->CreateSeries(_T("Taxes")));
		CBCGPChartBarSeries* pBarSeries3 = DYNAMIC_DOWNCAST(CBCGPChartBarSeries, pChart->CreateSeries(_T("Maintenance")));
		CBCGPChartBarSeries* pBarSeries4 = DYNAMIC_DOWNCAST(CBCGPChartBarSeries, pChart->CreateSeries(_T("Fuel")));

		pBarSeries1->AddDataPoint(m_arYears[4], 2);
		pBarSeries1->AddDataPoint(m_arYears[3], 3);
		pBarSeries1->AddDataPoint(m_arYears[2], 5);
		pBarSeries1->AddDataPoint(m_arYears[1], 3);
		pBarSeries1->AddDataPoint(m_arYears[0], 1);

		pBarSeries2->AddDataPoint(5);
		pBarSeries2->AddDataPoint(7);
		pBarSeries2->AddDataPoint(9);
		pBarSeries2->AddDataPoint(10);
		pBarSeries2->AddDataPoint(11);

		pBarSeries3->AddDataPoint(10);
		pBarSeries3->AddDataPoint(12);
		pBarSeries3->AddDataPoint(13);
		pBarSeries3->AddDataPoint(14);
		pBarSeries3->AddDataPoint(12);

		pBarSeries4->AddDataPoint(12);
		pBarSeries4->AddDataPoint(13);
		pBarSeries4->AddDataPoint(15);
		pBarSeries4->AddDataPoint(13);
		pBarSeries4->AddDataPoint(12);

		if (m_Feature == CDemoFeature::BCGP_StackedColumnSideBySide3D)
		{
			pBarSeries1->SetGroupID(0);
			pBarSeries2->SetGroupID(0);

			pBarSeries3->SetGroupID(1);
			pBarSeries4->SetGroupID(1);
		}
	}

	OnUpdateChart();
}

void CChartBar3DView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	CBCGPChartExampleView::OnHScroll(nSBCode, nPos, pScrollBar);

	if (pScrollBar != NULL)
	{
		OnUpdateChart();
	}
}

void CChartBar3DView::OnUpdateChart() 
{
	UpdateData();

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	// for 3D bars label position can't be changed and is always center
	BOOL bCanChangeLabelPos = m_Feature != CDemoFeature::BCGP_ClusteredBar3D && 
								m_Feature != CDemoFeature::BCGP_Bar3D && 
								m_Feature != CDemoFeature::BCGP_StackedBar3D && 
								m_Feature != CDemoFeature::BCGP_StackedBar1003D;

	m_wndDataLabelPosition.EnableWindow(m_bShowDataLabels && bCanChangeLabelPos);

	pChart->ShowAxisIntervalInterlacing(BCGP_CHART_X_PRIMARY_AXIS, FALSE);
	pChart->ShowAxisIntervalInterlacing(BCGP_CHART_Y_PRIMARY_AXIS);

	pChart->ShowDataLabels(m_bShowDataLabels);

	if (m_bShowDataLabels)
	{
		BCGPChartDataLabelOptions dataLabelOptions = pChart->GetDataLabelOptions();
		dataLabelOptions.m_position = (BCGPChartDataLabelOptions::LabelPosition)m_nDataLabelPosition;

		pChart->SetDataLabelsOptions(dataLabelOptions);
	}

	CBCGPBrush::BCGP_GRADIENT_TYPE type;

	switch (m_nFillGradientType)
	{
	case 0:
	default:
		type = (CBCGPBrush::BCGP_GRADIENT_TYPE)-1;
		break;

	case 1:
		type = CBCGPBrush::BCGP_NO_GRADIENT;
		break;

	case 2:
		type = CBCGPBrush::BCGP_GRADIENT_HORIZONTAL;
		break;

	case 3:
		type = CBCGPBrush::BCGP_GRADIENT_VERTICAL;
		break;

	case 4:
		type = pChart->GetChartCategory() == BCGPChartBar3D ? 
			CBCGPBrush::BCGP_GRADIENT_PIPE_HORIZONTAL : CBCGPBrush::BCGP_GRADIENT_PIPE_VERTICAL;
		break;
	}

	for (int i = 0; i < pChart->GetSeriesCount(); i++)
	{
		CBCGPChartBarSeries* pSeries = DYNAMIC_DOWNCAST(CBCGPChartBarSeries, pChart->GetSeries(i));
		ASSERT_VALID(pSeries);

		pSeries->SetDefaultFillGradientType(type);
	}
	
	pChart->SetThemeOpacity(100 - m_nTransparency);

	DWORD dwoFlags = CBCGPChartDiagram3D::DWO_OUTLINE_ALL;

	// Draw walls and floor:
	if (m_bShowWalls)
	{
		dwoFlags |= (CBCGPChartDiagram3D::DWO_DRAW_ALL_WALLS | CBCGPChartDiagram3D::DWO_DRAW_FLOOR);
	}

	m_wndThickWalls.EnableWindow(m_bShowWalls);

	pChart->GetDiagram3D()->SetDrawWallOptions((CBCGPChartDiagram3D::DrawWallOptions)dwoFlags);
	pChart->GetDiagram3D()->SetThickWallsAndFloor(m_bShowWalls && m_bThickWalls);

	pChart->SetDirty();
	pChart->Redraw();
}

void CChartBar3DView::OnRotate() 
{
	RotateChart(m_wndRotate.GetRotationObject()->GetClicked());
}
